 package net.go176.backpack.listener;
 
 import net.go176.backpack.core.DataManager;
import net.go176.backpack.core.MyDataHandlerGUI;
import net.go176.backpack.gui.OfflineInvHolder;
import net.go176.backpack.gui.OnlineInvHolder;
import net.go176.backpack.Backpack;


 import net.go176.backpack.gui.InventoryType;


 import org.bukkit.entity.HumanEntity;
 import org.bukkit.entity.Player;
 import org.bukkit.event.EventHandler;
 import org.bukkit.event.EventPriority;
 import org.bukkit.event.Listener;
 import org.bukkit.event.inventory.InventoryAction;
 import org.bukkit.event.inventory.InventoryClickEvent;
 import org.bukkit.event.inventory.InventoryCloseEvent;
 import org.bukkit.event.inventory.InventoryDragEvent;
 import org.bukkit.event.inventory.InventoryOpenEvent;
 import org.bukkit.event.player.PlayerDropItemEvent;
 import org.bukkit.event.player.PlayerInteractEntityEvent;
 import org.bukkit.event.player.PlayerInteractEvent;
 import org.bukkit.event.player.PlayerItemBreakEvent;
 import org.bukkit.event.player.PlayerItemConsumeEvent;
 import org.bukkit.event.player.PlayerPickupItemEvent;
 import org.bukkit.inventory.InventoryHolder;
 import org.bukkit.plugin.Plugin;
 
 public class GUIListener
   implements Listener {
   private Backpack p;
   
   public GUIListener(Backpack plugin) {
     this.p = plugin;
   }
   @EventHandler(priority = EventPriority.MONITOR)
   public void onInventoryClick(InventoryClickEvent e) {
     HumanEntity humanEntity1;
     if (e.getAction() == InventoryAction.NOTHING)
       return; 
     HumanEntity whoClicked = e.getWhoClicked();
     InventoryHolder holder = e.getInventory().getHolder();
     if (e.getInventory().equals(whoClicked.getEnderChest()))
       humanEntity1 = whoClicked; 
     if (humanEntity1 instanceof OnlineInvHolder) {
       if (!hasClickPermission(whoClicked)) {
         e.setCancelled(true);
         return;
       } 
       updateInventory((OnlineInvHolder)humanEntity1, whoClicked, false);
     } else if (humanEntity1 instanceof Player && humanEntity1.equals(whoClicked)) {
       updateViewerInventories((Player)humanEntity1);
     } 
   }
   @EventHandler(priority = EventPriority.MONITOR)
   public void onInventoryDrag(InventoryDragEvent e) {
     HumanEntity humanEntity1, whoClicked = e.getWhoClicked();
     InventoryHolder holder = e.getInventory().getHolder();
     if (e.getInventory().equals(whoClicked.getEnderChest()))
       humanEntity1 = whoClicked; 
     if (humanEntity1 instanceof OnlineInvHolder) {
       if (!hasClickPermission(whoClicked)) {
         e.setCancelled(true);
         return;
       } 
       updateInventory((OnlineInvHolder)humanEntity1, whoClicked, false);
     } else if (humanEntity1 instanceof Player && humanEntity1.equals(whoClicked)) {
       updateViewerInventories((Player)humanEntity1);
     } 
   }
   @EventHandler(priority = EventPriority.MONITOR)
   public void onInventoryClose(InventoryCloseEvent e) {
     HumanEntity humanEntity1, player = e.getPlayer();
     InventoryHolder holder = e.getInventory().getHolder();
     if (e.getInventory().equals(player.getEnderChest()))
       humanEntity1 = player; 
     if (humanEntity1 instanceof OfflineInvHolder) {
       if (!hasClickPermission(player))
         return; 
       saveOfflineInventory((OfflineInvHolder)humanEntity1);
     } else if (humanEntity1 instanceof Player && humanEntity1 == player) {
       updateViewerInventories((Player)humanEntity1);
     } 
   }
   @EventHandler(priority = EventPriority.MONITOR)
   public void onInventoryOpen(InventoryOpenEvent e) {
     HumanEntity human = e.getPlayer();
     if (human instanceof Player)
       updateViewerInventories((Player)human); 
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerInteract(PlayerInteractEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerInteractEntity(PlayerInteractEntityEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerPickupItem(PlayerPickupItemEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerDropItem(PlayerDropItemEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerItemBreak(PlayerItemBreakEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   @EventHandler(priority = EventPriority.MONITOR)
   public void onPlayerItemConsume(PlayerItemConsumeEvent e) {
     updateViewerInventories(e.getPlayer());
   }
   
   private static boolean hasClickPermission(HumanEntity whoClicked) {
     return !(!whoClicked.hasPermission("mybackpack.inventory.click") && !whoClicked.hasPermission("mybackpack.inventory.*"));
   }
   
   private void updateInventory(final OnlineInvHolder holder, final HumanEntity viewer, final boolean close) {
     this.p.getCoreHelper().getDataHandlerGUI(holder.invType).readOnlineInv(holder.inv, holder.player);
     this.p.getServer().getScheduler().runTask((Plugin)this.p, new Runnable()
         {
           public void run()
           {
             GUIListener.this.p.getCoreHelper().getDataHandlerGUI(holder.invType).onUpdateOnlineInv(holder.inv, holder.player, viewer);
             if (close) {
               DataManager.PlayerStatus status = GUIListener.this.p.getDataManager().getStatus(holder.player.getName());
               if (status == null)
                 return; 
               status.getOnlineHolder(holder.invType);
             } 
           }
         });
   }
 
   
   private void updateViewerInventories(final Player holder) {
     this.p.getServer().getScheduler().runTask((Plugin)this.p, new Runnable() {
           public void run() {
             byte b;
             int i;
             InventoryType[] arrayOfInventoryType;
             for (i = (arrayOfInventoryType = InventoryType.values()).length, b = 0; b < i; ) { InventoryType invType = arrayOfInventoryType[b];
               MyDataHandlerGUI handler = GUIListener.this.p.getCoreHelper().getDataHandlerGUI(invType);
               if (handler != null) {
                 
                 DataManager.PlayerStatus status = GUIListener.this.p.getDataManager().getStatus(holder.getName());
                 if (status != null) {
                   
                   OnlineInvHolder onlineHolder = status.getOnlineHolder(invType);
                   if (onlineHolder != null)
                   {
                     handler.readOnlineInv(onlineHolder.inv, onlineHolder.player); } 
                 } 
               } 
               b++; }
           
           }
         });
   } private void saveOfflineInventory(final OfflineInvHolder holder) {
     this.p.getServer().getScheduler().runTask((Plugin)this.p, new Runnable()
         {
           public void run()
           {
             GUIListener.this.p.getDataManager().onCloseOfflineInv(holder);
           }
         });
   }
 }


/* Location:              D:\User\Xuan\Downloads\MyBackpack1.2.5.jar!\net\saralab\mybackpack\listener\GUIListener.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.1.3
 */